package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.HealthIndicator;
import com.health_predict_springboot.service.HealthIndicatorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/health-indicator")
@Tag(name = "健康指标管理", description = "健康指标相关接口")
public class HealthIndicatorController {

    @Autowired
    private HealthIndicatorService healthIndicatorService;

    @Operation(summary = "根据ID查询健康指标", description = "通过指标ID获取健康指标详情")
    @GetMapping("/getById")
    public HealthIndicator getHealthIndicatorById(@RequestParam Long indicatorId) {
        return healthIndicatorService.getIndicatorById(indicatorId);
    }

    @Operation(summary = "根据用户ID查询健康指标", description = "通过用户ID获取该用户的所有健康指标记录")
    @GetMapping("/getByUserId")
    public List<HealthIndicator> getHealthIndicatorsByUserId(@RequestParam Long userId) {
        return healthIndicatorService.getIndicatorsByUserId(userId);
    }

    @Operation(summary = "获取所有健康指标", description = "获取系统中所有的健康指标记录")
    @GetMapping("/getAll")
    public List<HealthIndicator> getAllHealthIndicators() {
        return healthIndicatorService.getAllIndicators();
    }

    @Operation(summary = "分页查询健康指标", description = "分页获取健康指标记录")
    @GetMapping("/getByPage")
    public List<HealthIndicator> getHealthIndicatorsByPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("pageSize", pageSize);
        return healthIndicatorService.getIndicatorsByPage(params);
    }

    @Operation(summary = "添加健康指标", description = "新增一条健康指标记录")
    @PostMapping("/add")
    public int addHealthIndicator(@RequestBody HealthIndicator healthIndicator) {
        // 设置提交时间和更新时间
        Date now = new Date();
        healthIndicator.setSubmitTime(now);
        healthIndicator.setUpdateTime(now);
        return healthIndicatorService.addIndicator(healthIndicator);
    }

    @Operation(summary = "更新健康指标", description = "更新已有的健康指标记录")
    @PutMapping("/update")
    public int updateHealthIndicator(@RequestBody HealthIndicator healthIndicator) {
        // 设置更新时间
        healthIndicator.setUpdateTime(new Date());
        return healthIndicatorService.updateIndicator(healthIndicator);
    }

    @Operation(summary = "删除健康指标", description = "通过ID删除健康指标记录")
    @DeleteMapping("/delete")
    public int deleteHealthIndicator(@RequestParam Long indicatorId) {
        return healthIndicatorService.deleteIndicator(indicatorId);
    }

    @Operation(summary = "按时间范围查询健康指标", description = "获取指定时间范围内的健康指标记录")
    @GetMapping("/getByTimeRange")
    public List<HealthIndicator> getHealthIndicatorsByTimeRange(
            @RequestParam Long userId,
            @RequestParam Date startTime,
            @RequestParam Date endTime) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return healthIndicatorService.getIndicatorsByUserIdAndTimeRange(params);
    }

    @Operation(summary = "查询异常指标", description = "获取用户的异常健康指标记录")
    @GetMapping("/getAbnormalIndicators")
    public List<HealthIndicator> getAbnormalIndicators() {
        return healthIndicatorService.getAbnormalIndicators();
    }

    @Operation(summary = "计算BMI", description = "根据身高和体重计算BMI值")
    @GetMapping("/calculateBMI")
    public double calculateBMI(@RequestParam Integer height, @RequestParam Double weight) {
        return healthIndicatorService.calculateBMI(height, weight);
    }

    @Operation(summary = "判断指标是否异常", description = "判断特定健康指标是否异常")
    @PostMapping("/isIndicatorAbnormal")
    public int isIndicatorAbnormal(@RequestBody HealthIndicator indicator) {
        return healthIndicatorService.judgeAbnormal(indicator);
    }
}